import 'dart:convert';

import 'package:fluent_ui/fluent_ui.dart';
import 'package:http/http.dart' as http;

final domain = 'http://localhost:3000';

class Result {
  final int code;
  final dynamic data;
  final String? message;
  final int? timestamp;

  Result({
    required this.code,
    required this.data,
    required this.message,
    required this.timestamp,
  });
}

Result _handleResponse(http.Response response) {
  // final flyoutController = FlyoutController();
  //
  // FlyoutTarget(
  //   controller: flyoutController,
  //   child: Button(
  //     child: const Text('Show TeachingTip'),
  //     onPressed: () {
  //
  //     },
  //   ),
  // );
  //
  // showTeachingTip(
  //   flyoutController: flyoutController,
  //   nonTargetedAlignment: Alignment.bottomCenter,
  //   builder: (context) {
  //     return TeachingTip(
  //       title: const Text('Tip'),
  //       subtitle: const Text('Operation complete successfully.'),
  //       buttons: [
  //         Button(
  //           onPressed: () => flyoutController.close(),
  //           child: const Text('Close'),
  //         ),
  //       ],
  //     );
  //   },
  // );

  // 如果请求成功，解析JSON数据
  if (response.statusCode == 200) {
    var data = jsonDecode(response.body);
    print(data);
    return Result(code: data['code'], data: data['data'], message: data['message'], timestamp: data['timestamp']);
  }

  // 如果请求失败，打印错误信息
  print('Request failed with status: ${response.statusCode}.');
  return Result(code: response.statusCode, data: null, message: response.reasonPhrase, timestamp: null);
}

// 模拟发送注册验证码的请求
Future<Result> sendRegisterVerificationCode(String phoneNumber) async {
  final response = await http.get(Uri.parse('$domain/send_register_verification_code?phoneNumber=$phoneNumber'));
  return _handleResponse(response);
}

// 模拟发送注册的请求
Future<Result> registerUser(Map<String, String> user) async {
  final response = await http.post(
    Uri.parse('$domain/register'),
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode(user),
  );
  return _handleResponse(response);
}

// Future<void> fetchData() async {
//   final response = await http.get(
//       Uri.parse('https://jsonplaceholder.typicode.com/posts/1'),
//   );
//
//   if (response.statusCode == 200) {
//     // 如果请求成功，解析JSON数据
//     var data = jsonDecode(response.body);
//     print(data);
//   } else {
//     // 如果请求失败，打印错误信息
//     print('Request failed with status: ${response.statusCode}.');
//   }
// }

// Future<void> postData() async {
//   final response = await http.post(
//     Uri.parse('https://jsonplaceholder.typicode.com/posts'),
//     headers: <String, String>{
//       'Content-Type': 'application/json; charset=UTF-8',
//     },
//     body: jsonEncode(<String, String>{
//       'title': 'The Flutter Way',
//       'body': 'My first post!',
//       'userId': '1',
//     }),
//   );
//
//   if (response.statusCode == 201) {
//     // 如果请求成功，解析返回的数据
//     var data = jsonDecode(response.body);
//     print(data);
//   } else {
//     // 如果请求失败，打印错误信息
//     print('Request failed with status: ${response.statusCode}.');
//   }
// }
